package com.lkd.util;

import com.lkd.common.VMSystem;
import com.lkd.entity.OrderEntity;
import com.lkd.vo.PartnerVO;
import com.lkd.vo.PayVO;
import com.lkd.vo.SkuVO;
import com.lkd.vo.VmVO;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class OrderEntityBuilder {
    private PayVO payVO;
    private VmVO vmVO;
    private SkuVO sku;
    private PartnerVO partner;

    private OrderEntity orderEntity = new OrderEntity();

    public OrderEntityBuilder(PayVO payVO,VmVO vmVO,SkuVO sku,PartnerVO partner){
        this.payVO = payVO;
        this.vmVO = vmVO;
        this.sku = sku;
        this.partner = partner;
    }

    public OrderEntityBuilder buildVM(){
        BeanUtils.copyProperties(vmVO,orderEntity);
        orderEntity.setAddr(vmVO.getNodeAddr());
        return this;
    }

    public OrderEntityBuilder buildSku(){
        BeanUtils.copyProperties(sku,orderEntity);
        orderEntity.setAmount(sku.getRealPrice());
        orderEntity.setSkuId(Long.valueOf(payVO.getSkuId()));
        orderEntity.setStatus(VMSystem.ORDER_STATUS_CREATE);
        return this;
    }

    public OrderEntityBuilder buildPayVO(){
        String orderNo = payVO.getInnerCode()+System.nanoTime()+"";
        orderEntity.setOrderNo(orderNo);
        orderEntity.setInnerCode(payVO.getInnerCode());
        orderEntity.setOpenId(payVO.getOpenId());
        return this;
    }

    public OrderEntityBuilder buildPartner(){
        BigDecimal bg = new BigDecimal(orderEntity.getAmount());
        int bill = bg.multiply(new BigDecimal(partner.getRatio()).divide(new BigDecimal(100), 0, RoundingMode.HALF_UP)).intValue();
        orderEntity.setBill(bill);
        return this;
    }

    public OrderEntityBuilder buildOrderDetile(){
        orderEntity.setPayType("2");
        orderEntity.setPayStatus(VMSystem.PAY_STATUS_NOPAY);
        return this;
    }

    public OrderEntity builder(){
        return orderEntity;
    }
}
